#include "Storage/SqlSvrConnectionConfig.hpp"
#include "CaseDB/SqlSvrConnection.hpp"
#include <sstream>

namespace Storage
{

const std::string SqlSvrConnectionConfig::TYPENAME( "Microsoft SQL Server");

const std::string SqlSvrConnectionConfig::SERVER_NAME_ATTR("Server");
const std::string SqlSvrConnectionConfig::DATABASE_NAME_ATTR("Database");
const std::string SqlSvrConnectionConfig::USER_NAME_ATTR("User");
const std::string SqlSvrConnectionConfig::PASSWORD_ATTR("Password");



SqlSvrConnectionConfig::SqlSvrConnectionConfig( const std::string& name )
	: ConnectionConfig( TYPENAME, name)
{
}

// ---------------------------------------------------------------------------------------------------------------------

const std::string& SqlSvrConnectionConfig::serverName() const
{
	return const_cast<AttributeCollection&>(attributes_)[ SERVER_NAME_ATTR];
}

// ---------------------------------------------------------------------------------------------------------------------

void SqlSvrConnectionConfig::setServerName( const std::string& newSvrName )
{
	attributes_[ SERVER_NAME_ATTR] = newSvrName;
}

// ---------------------------------------------------------------------------------------------------------------------

const std::string& SqlSvrConnectionConfig::databaseName() const
{
	return const_cast<AttributeCollection&>(attributes_)[DATABASE_NAME_ATTR];
}

// ---------------------------------------------------------------------------------------------------------------------

void SqlSvrConnectionConfig::setDatabaseName( const std::string& newDbName )
{
	attributes_[DATABASE_NAME_ATTR] = newDbName;
}

// ---------------------------------------------------------------------------------------------------------------------

const std::string& SqlSvrConnectionConfig::userName() const
{
	return const_cast<AttributeCollection&>(attributes_)[USER_NAME_ATTR];
}

// ---------------------------------------------------------------------------------------------------------------------

void SqlSvrConnectionConfig::setUserName( const std::string& newUserName )
{
	attributes_[USER_NAME_ATTR] = newUserName;
}

// ---------------------------------------------------------------------------------------------------------------------

const std::string& SqlSvrConnectionConfig::password() const
{
	return const_cast<AttributeCollection&>(attributes_)[PASSWORD_ATTR];
}

// ---------------------------------------------------------------------------------------------------------------------

void SqlSvrConnectionConfig::setPassword( const std::string& newPassword )
{
	attributes_[PASSWORD_ATTR] = newPassword;
}

// ---------------------------------------------------------------------------------------------------------------------

std::string SqlSvrConnectionConfig::connectionString() const
{
	std::ostringstream os;
	os	<< "DRIVER=SQL Server;" 
		<< "UID=" << this->userName() << ";"
		<< "DATABASE=" << this->databaseName() << ";"
		<< "SERVER=" << this->serverName() << ";"
		<< "PWD=" << this->password();
	return os.str();
}

} // namespace Storage
